Current Location: Home> Function Categories> property_exists

property_exists

Check if an object or class has this property
Name:property_exists
Category:Classes and Objects
Programming Language:php
One-line Description:Checks whether an object or class has the specified attribute

Function name: property_exists()

Function Description: property_exists() function checks whether an object or class has the specified attribute.

parameter:

  • $class: Required. The class name or object to be checked.
  • $property: Required. The attribute name to check.

Return value:

  • Returns true if the property exists and is accessible.
  • If the property does not exist or is inaccessible, false is returned.

Applicable version: PHP 4, PHP 5, PHP 7

Usage example:

  1. Check whether the class has the specified attribute:
 class MyClass { public $name = "John"; private $age = 25; } $object = new MyClass(); if (property_exists($object, 'name')) { echo "The 'name' property exists."; } else { echo "The 'name' property does not exist."; } // 输出:The 'name' property exists.
  1. Check whether the object has the specified attribute:
 class MyClass { public $name = "John"; private $age = 25; } $object = new MyClass(); if (property_exists($object, 'age')) { echo "The 'age' property exists."; } else { echo "The 'age' property does not exist."; } // 输出:The 'age' property does not exist.
  1. Check whether the class name has the specified static attribute:
 class MyClass { public static $name = "John"; private static $age = 25; } if (property_exists('MyClass', 'name')) { echo "The 'name' static property exists."; } else { echo "The 'name' static property does not exist."; } // 输出:The 'name' static property exists.
  1. Check whether the class name has the specified static private attribute:
 class MyClass { public static $name = "John"; private static $age = 25; } if (property_exists('MyClass', 'age')) { echo "The 'age' static property exists."; } else { echo "The 'age' static property does not exist."; } // 输出:The 'age' static property does not exist.
Related
Similar Functions
Popular Articles